package com.zxf.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zxf.dao.JedisClient;
import com.zxf.dao.JedisClientSingle;
import com.zxf.dao.UsersMapper;
import com.zxf.pojo.Params;
import com.zxf.pojo.Users;

@Service  
//默认将类中的所有函数纳入事务管理.  
@Transactional  
public class UserService2 {
	@Autowired
	private JedisClient jedisClient;

	@Value("${REDIS_USER_KEY}")
	private String REDIS_USER_KEY;
	@Value("${USER_BASE_INFO_KEY}")
	private  String USER_BASE_INFO_KEY;
	
	 
	    @Autowired  
	    private UsersMapper userMapper;
	    
	   // @Resource
		//public JedisClientSingle jedisClientSingle;
	    
	    
	    @Resource
	    protected RedisTemplate<Serializable, Serializable> redisTemplate;
	  
	  
	 public void insertUser(Users user,Map<String,Object> param) {  
	        //新增用户  
	        userMapper.insert(user);  
	 }

	public Users getId(int userid) {
		Users users = new Users();
		users = userMapper.selectByPrimaryKey(userid);
		return users;
	}

	public List getUserList() {
		// 用户列表
		List  userList = userMapper.getUserList();
		return userList;
	}

	public Map<String, Object> getUserList2() {
		// TODO Auto-generated method stub
		Map<String, Object> userMap = new HashMap<String, Object>();
		userMap= userMapper.getUserList2();
		return userMap;
	}

	public List getUserList3(int pagenum, int numbers) {
		// TODO Auto-generated method stub
		List userMap = new ArrayList();
		Params pams = new Params();
		pams.setPagenum(pagenum);
		pams.setPagesize(numbers);
		//userMap= userMapper.getUserList3(arguMap);
		userMap= userMapper.getUserList3(pams);
		return userMap;
	}

	public int getusertotle() {
		// 用户总数
		int totle = userMapper.gettotle();
		return totle;
	}

	public Users selectusers(Users users) {
		// TODO Auto-generated method stub
		return userMapper.selectusers(users);
	}

	//添加用户到redis
	public boolean add(Users users) {
		// TODO Auto-generated method stub
		
		boolean result = saveUser(users);
		return result;
		
	}

	//删除用户到redis
	public void delete(int parseInt) {
		// TODO Auto-generated method stub
		
	}
	public boolean saveUser(final Users user) {
		//jedisClientSingle.set("users", user.getUserid().toString());
		//jedisClient.set(REDIS_USER_KEY+":"+user.getUserid()+":"+USER_BASE_INFO_KEY, REDIS_USER_KEY);
		//设置key过期时间，因为要设置过期时间，只能在key上设置，所以不能用hash
		//jedisClient.expire(REDIS_USER_KEY+":"+user.getUserid()+":"+USER_BASE_INFO_KEY,ITEM_EXPIRE_SECOND);
        redisTemplate.execute(new RedisCallback<Object>() {

            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                connection.set(redisTemplate.getStringSerializer().serialize("user.uid"+ user.getUserid()),
                               redisTemplate.getStringSerializer().serialize("user.name"+user.getUsername()));
                return null;
            }

			
        });
		return false;
    }
}
